package com.zzyl.service.customer.impl;


import cn.hutool.core.date.LocalDateTimeUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.ReservationDto;
import com.zzyl.entity.Reservation;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.CustomerReservationMapper;
import com.zzyl.service.customer.CustomerReservationService;
import com.zzyl.utils.UserThreadLocal;
import com.zzyl.vo.CountShengYuVo;
import com.zzyl.vo.ReservationVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CustomerReservationServiceImpl implements CustomerReservationService {
    @Autowired
    private CustomerReservationMapper customerReservationMapper;

    //1.查询取消预约数量
    @Override
    public Integer getCancelledCount(Long userId) {
        LocalDateTime startTime = LocalDateTime.now().
                withHour(0).
                withMinute(0).
                withSecond(0);
        LocalDateTime endTime = LocalDateTime.now().
                withHour(23).
                withMinute(59).
                withSecond(59);
        return customerReservationMapper.getCancelledCount(userId,startTime,endTime);
    }
    //2.查询每个时间段剩余预约次数
    @Override
    public List<CountShengYuVo> countByTime(Long time) {
        LocalDateTime time1 = LocalDateTimeUtil.of(time);
        LocalDateTime startTime = time1.withHour(0).withMinute(0).withSecond(0);
        LocalDateTime endTime = time1.withHour(23).withMinute(59).withSecond(59);
        return customerReservationMapper.countByTime(startTime,endTime);
    }

    //定时任务，把超时的status变成 3 （已超时）
    @Override
    public void updateStatusByTask(LocalDateTime now) {
        customerReservationMapper.updateStatusByTask(now);
    }

    //3.新增预约
    @Override
    public void insert(ReservationDto reservationDto) {
        //判断当前用户是否已经取消过三次，报错可以去常量类里面找
        Integer count = getCancelledCount(UserThreadLocal.getUserId());
        if(count>=3){
            throw new BaseException(BasicEnum.RESERVATION_CANCEL_COUNT_UPPER_LIMIT);
        }
        Reservation reservation = new Reservation();
        BeanUtils.copyProperties(reservationDto,reservation);
        reservation.setStatus(0);
        customerReservationMapper.insert(reservation);
    }


    @Override
    public PageResponse<ReservationVo> selectByPage(Integer status, Integer pageNum, Integer pageSize) {
        Long userId = UserThreadLocal.getUserId();
        PageHelper.startPage(pageNum, pageSize);
        Page<ReservationVo> page = customerReservationMapper.selectByPage(userId);
        PageResponse<ReservationVo> pageResponse = PageResponse.of(page,ReservationVo.class);
        return pageResponse;
    }
    @Override
    public void updateStatusById(Long id) {
        customerReservationMapper.updateStatusById(id);
    }

}
